package cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.service;

import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.ParticipatingConference;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.mapper.ParticipatingConferenceMapper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.util.SnowflakeIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParticipatingConferenceService {

    @Autowired
    private ParticipatingConferenceMapper participatingConferenceMapper;

    @Autowired
    private SnowflakeIdGenerator idGenerator;

    public String save(ParticipatingConference participatingConference) {
        // 生成雪花ID
        long id = idGenerator.generateId();
        participatingConference.setId(id);

        if (participatingConferenceMapper.save(participatingConference)) {
            return "保存成功";
        }
        return "保存失败";
    }

    public String delete(Long participatingConferenceId) {
        if (participatingConferenceMapper.delete(participatingConferenceId)) {
            return "删除成功";
        }
        return "删除失败";
    }

    public String update(ParticipatingConference participatingConference) {
        try {
            if (participatingConferenceMapper.update(participatingConference)) {
                return "更新成功";
            }
            return "更新失败";
        } catch (Exception e) {
            throw new RuntimeException("更新参会记录失败", e);
        }
    }

    public PageInfo<ParticipatingConference> get(int pageNum, int pageSize, ParticipatingConference participatingConference) {
        PageHelper.startPage(pageNum, pageSize);
        List<ParticipatingConference> participatingConferenceList = participatingConferenceMapper.get(participatingConference);
        return PageInfo.of(participatingConferenceList);
    }
}